﻿
using System.Collections.Generic;

namespace LxCFD
{
    public class DistancePDEUnit : TypicalEquationUnit
    {
        public int MaxStep = 200;
        public int CurrentStep = 0;
        public ResidualMonitor residualItem;
        public List<WallDistance> DistanceInZone;
        public List<ConnectHeadBase> DistanceInContact;
        public void CalDistance(List<WallDistance> distanceInZone, List<ConnectHeadBase> distanceInContact)
        {
            this.DistanceInZone = distanceInZone;
            this.DistanceInContact = distanceInContact;
            residualItem = new ResidualMonitor();
            List<EquationBase> list = new List<EquationBase>();
            foreach (var walldis in DistanceInZone)
            {
                PossionEquation wdcal = walldis.MakePDECalulator();
                list.Add(wdcal);
                residualItem.Add(wdcal);
            }
            CreateUnit(list, new List<ConnectBase>(), SolverTypes.AMGF);
            do
            {
                Solve_Steady();

            } while (isContinue());

            MultiThreads.RunCalTasks(CalDistanceByPDE);
        }
        public void CalDistanceByPDE(int taskIndex)
        {
            for (int i1 = 0; i1 < DistanceInZone.Count; i1++)
            {
                DistanceInZone[i1].CalDistanceFromPDE(Equations[i1], taskIndex);
            }
        }
        public bool isContinue()
        {
            CurrentStep++;
            if (CurrentStep < 3)
            {
                return true;
            }
            residualItem.RunMonitor();           //residualItem.FinishSteps(out bool hasResult, out double rc);// CalUnit.GetMaxNormalRc();
            if (residualItem.CurrentResult < 1e-4)
            {
                LxConsole.Info("完成计算壁面距离。");
                return false;
            }
            if (CurrentStep > MaxStep)
            {
                LxConsole.Error("计算壁面距离时未收敛！");
                return false;
            }
            return true;
        }
    }
}
//TasksControl.RunCalTasks(g.SolvePDE, g.isContinue);
//TasksControl.RunCalTasks(g.CalDistanceByPDE);